<template>
  <el-dialog
    :title="!dataForm.administorId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户名" prop="username">
      <el-input v-model="dataForm.username" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getAdministorById, saveOrUpdateAdministor } from '@/api/administor'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        administorId: 0,
        username: '',
        password: ''
      },
      dataRule: {
        username: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.administorId = id || 0
      this.visible = true
      this.$nextTick(async () => {
        this.$refs.dataForm.resetFields()
        if (this.dataForm.administorId) {
          const { data } = await getAdministorById(this.dataForm.administorId)
          if (data && data.code === 0) {
            this.dataForm.username = data.administor.username
            this.dataForm.password = data.administor.password
          }
          // this.$http({
          //   url: `/administor/info/${this.dataForm.administorId}`,
          //   method: 'get'
          // }).then(({ data }) => {
          //   if (data && data.code === 0) {
          //     this.dataForm.username = data.administor.username
          //     this.dataForm.password = data.administor.password
          //   }
          // })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs.dataForm.validate(async (valid) => {
        if (valid) {
          const administor = {
            administorId: this.dataForm.administorId || undefined,
            username: this.dataForm.username,
            password: this.dataForm.password
          }
          const { data } = await saveOrUpdateAdministor(this.dataForm.administorId, administor)
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          } else {
            this.$message.error(data.msg)
          }

          // this.$http({
          //   url: `/administor/${!this.dataForm.administorId ? 'save' : 'update'}`,
          //   method: 'post',
          //   data: {
          //     administorId: this.dataForm.administorId || undefined,
          //     username: this.dataForm.username,
          //     password: this.dataForm.password
          //   }
          // }).then(({ data }) => {
          //   if (data && data.code === 0) {
          //     this.$message({
          //       message: '操作成功',
          //       type: 'success',
          //       duration: 1500,
          //       onClose: () => {
          //         this.visible = false
          //         this.$emit('refreshDataList')
          //       }
          //     })
          //   } else {
          //     this.$message.error(data.msg)
          //   }
          // })
        }
      })
    }
  }
}
</script>
